package com.xjh.config;

import lombok.extern.slf4j.Slf4j;
import org.mybatis.generator.api.CommentGenerator;
import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.*;
import org.mybatis.generator.api.dom.xml.XmlElement;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import java.util.Set;

/**
 * @author siti
 * @createDate 2020/2/17
 * @description 自动生成实现
 */
@Slf4j
public class MyCommentGenerator implements CommentGenerator {
    private Properties properties;


    public MyCommentGenerator() {
        properties = new Properties();
    }

    @Override
    public void addConfigurationProperties(Properties properties) {
        log.info("当前执行方法："+Thread.currentThread().getStackTrace()[1].getMethodName());
        this.properties.putAll(properties);
    }

    @Override
    public void addFieldComment(Field field, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) {
        log.info("当前执行方法："+Thread.currentThread().getStackTrace()[1].getMethodName());
    }

    @Override
    public void addFieldComment(Field field, IntrospectedTable introspectedTable) {
        log.info("当前执行方法："+Thread.currentThread().getStackTrace()[1].getMethodName());
    }

    @Override
    public void addModelClassComment(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
        log.info("当前执行方法："+Thread.currentThread().getStackTrace()[1].getMethodName());
        String author = properties.getProperty("author");

        String dateFormat = properties.getProperty("dateFormat", "yyyy-MM-dd");

        SimpleDateFormat dateFormatter = new SimpleDateFormat(dateFormat);
        // 获取表注释
        String remarks = introspectedTable.getRemarks();

        topLevelClass.addJavaDocLine("/**");

        topLevelClass.addJavaDocLine(" * " + remarks);

        topLevelClass.addJavaDocLine(" *");

        topLevelClass.addJavaDocLine(" * @author " + author);

        topLevelClass.addJavaDocLine(" * @date " + dateFormatter.format(new Date()));

        topLevelClass.addJavaDocLine(" */");
    }

    @Override
    public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable) {
        log.info("当前执行方法："+Thread.currentThread().getStackTrace()[1].getMethodName());
    }

    @Override
    public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable, boolean b) {
        log.info("当前执行方法："+Thread.currentThread().getStackTrace()[1].getMethodName());
    }

    @Override
    public void addEnumComment(InnerEnum innerEnum, IntrospectedTable introspectedTable) {
        log.info("当前执行方法："+Thread.currentThread().getStackTrace()[1].getMethodName());
    }

    @Override
    public void addGetterComment(Method method, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) {
        log.info("当前执行方法："+Thread.currentThread().getStackTrace()[1].getMethodName());
    }

    @Override
    public void addSetterComment(Method method, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) {
        log.info("当前执行方法："+Thread.currentThread().getStackTrace()[1].getMethodName());
    }

    @Override
    public void addGeneralMethodComment(Method method, IntrospectedTable introspectedTable) {
        log.info("当前执行方法："+Thread.currentThread().getStackTrace()[1].getMethodName());
    }

    @Override
    public void addJavaFileComment(CompilationUnit compilationUnit) {
        log.info("当前执行方法："+Thread.currentThread().getStackTrace()[1].getMethodName());
    }

    @Override
    public void addComment(XmlElement xmlElement) {
        log.info("当前执行方法："+Thread.currentThread().getStackTrace()[1].getMethodName());
    }

    @Override
    public void addRootComment(XmlElement xmlElement) {
        log.info("当前执行方法："+Thread.currentThread().getStackTrace()[1].getMethodName());
    }

    @Override
    public void addGeneralMethodAnnotation(Method method, IntrospectedTable introspectedTable, Set<FullyQualifiedJavaType> set) {
        log.info("当前执行方法："+Thread.currentThread().getStackTrace()[1].getMethodName());
    }

    @Override
    public void addGeneralMethodAnnotation(Method method, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn, Set<FullyQualifiedJavaType> set) {
        log.info("当前执行方法："+Thread.currentThread().getStackTrace()[1].getMethodName());
    }

    @Override
    public void addFieldAnnotation(Field field, IntrospectedTable introspectedTable, Set<FullyQualifiedJavaType> set) {
        log.info("当前执行方法："+Thread.currentThread().getStackTrace()[1].getMethodName());
    }

    @Override
    public void addFieldAnnotation(Field field, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn, Set<FullyQualifiedJavaType> set) {
        // 获取列注释
        String remarks = introspectedColumn.getRemarks();

        field.addJavaDocLine("/**");

        field.addJavaDocLine(" * " + remarks);

        field.addJavaDocLine(" */");
        log.info("当前执行方法："+Thread.currentThread().getStackTrace()[1].getMethodName());
    }

    @Override
    public void addClassAnnotation(InnerClass innerClass, IntrospectedTable introspectedTable, Set<FullyQualifiedJavaType> set) {
        log.info("当前执行方法："+Thread.currentThread().getStackTrace()[1].getMethodName());
    }
}
